package com.shop2cn.iapi.sqoptrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.parameter.req.RejectRefundReq;
import com.ymatou.iapi.optrade.parameter.vo.SalesRefundInfoVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.optrade.service.RejectRefundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;

/**
 * 拒绝退款接口 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_RejectRefund {

	private static RejectRefundReq salesrefundfailreq;
	private static RejectRefundCall salesrefundfailCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("拒绝退款接口 ");
	}

	@Before
	public void caseUp() {
		salesrefundfailreq = new RejectRefundReq();
		salesrefundfailCall = new RejectRefundCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(salesrefundfailCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectRefund_001() {
		Logger.start(true, "拒绝退货成功");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			// 非贝海发货
			TradingCallService.delivery(order.getSellerId(),
					order.getOrderId(), false);

			ApplyRefundReq applyRefundReq = RefundService.applyRefund(order);

			salesrefundfailreq.setOrderId(applyRefundReq.getOrderId());

			for (SalesRefundInfoVo salesRefundInfoVo : applyRefundReq
					.getSalesRefundInfoList()) {

				salesrefundfailreq.setRefundBillNo(salesRefundInfoVo
						.getRefundBillNo());

				salesrefundfailCall.setData(salesrefundfailreq);
				salesrefundfailCall.callService();

				Logger.verifyEquals("true",
						salesrefundfailCall.getString("success"),
						"验证拒绝退货成功接口返回正常");
			}

			if (salesrefundfailCall.getString("success").equals("true")) {

				int i = 0;
				// 数据库验证
				for (SalesRefundInfoVo refund : applyRefundReq
						.getSalesRefundInfoList()) {

					String refundBillNo = refund.getRefundBillNo();

					RefundBillPo expectedRefundBillPo = new RefundBillPo();

					expectedRefundBillPo.setRefundBillNo(refundBillNo);

					expectedRefundBillPo.setTopspeedRefund(false);

					expectedRefundBillPo.setRefundStatus(0);

					expectedRefundBillPo.setRefundBillStatus(-10);

					RefundBillPo actualRefundBillPo = orderShardingWapper
							.selectRefundBillByBillNo(refundBillNo);

					RefundService.verifyRefundBillDb(expectedRefundBillPo,
							actualRefundBillPo);

					i = i + 1;
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectRefund_002() {
		Logger.start(false, "订单号不存在");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			ApplyRefundReq applyRefundReq = RefundService.applyRefund(order);

			salesrefundfailreq.setOrderId(111111);
			salesrefundfailreq.setRefundBillNo(applyRefundReq
					.getSalesRefundInfoList().get(0).getRefundBillNo());
			salesrefundfailCall.setData(salesrefundfailreq);
			salesrefundfailCall.callService();

			Logger.verifyEquals("false",
					salesrefundfailCall.getString("success"), "验证拒绝退货成功接口返回");

			Logger.verifyEquals(String.format("ERR_REFUNDBILL_NOTEXISTS",
					salesrefundfailreq.getRefundBillNo()), salesrefundfailCall
					.getString("errorCode"), "验证更新退货退款接口errorCode");

			Logger.verifyEquals(
					String.format("退款单%s不存在",
							salesrefundfailreq.getRefundBillNo()),
					salesrefundfailCall.getString("errorMessage"),
					"验证更新退货退款接口Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectRefund_003() {
		Logger.start(false, "退款单号不存在");
		try {

			salesrefundfailreq.setOrderId(111111);
			salesrefundfailreq.setRefundBillNo("sdfsd");
			salesrefundfailCall.setData(salesrefundfailreq);
			salesrefundfailCall.callService();

			Logger.verifyEquals("false",
					salesrefundfailCall.getString("success"), "验证拒绝退货成功接口返回");

			Logger.verifyEquals(String.format("ERR_REFUNDBILL_NOTEXISTS",
					salesrefundfailreq.getRefundBillNo()), salesrefundfailCall
					.getString("errorCode"), "验证更新退货退款接口errorCode");

			Logger.verifyEquals(
					String.format("退款单%s不存在",
							salesrefundfailreq.getRefundBillNo()),
					salesrefundfailCall.getString("errorMessage"),
					"验证更新退货退款接口Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectRefund_004() {
		Logger.start(true, "重复拒绝退货");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			ApplyRefundReq applyRefundReq = RefundService
					.rejectRefundBill(order);

			salesrefundfailreq.setOrderId(applyRefundReq.getOrderId());

			salesrefundfailreq.setRefundBillNo(applyRefundReq
					.getSalesRefundInfoList().get(0).getRefundBillNo());

			salesrefundfailCall.setData(salesrefundfailreq);
			salesrefundfailCall.callService();

			Logger.verifyEquals("false",
					salesrefundfailCall.getString("success"), "验证拒绝退货成功接口返回");

			Logger.verifyEquals(
					String.format("ERR_STATUS_NOTMATCH",
							salesrefundfailreq.getRefundBillNo()),
					salesrefundfailCall.getString("errorCode"),
					"验证更新退货退款接口errorCode");

			Logger.verifyEquals(
					String.format("退款单%s状态不匹配",
							salesrefundfailreq.getRefundBillNo()),
					salesrefundfailCall.getString("errorMessage"),
					"验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RejectRefund_005() {
		Logger.start(true, "拒绝极速退货成功");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderAccept();

			Order order = placeOrderResp.getOrderList().get(0);

			ApplyRefundReq applyRefundReq = RefundService.applyRefund(order);

			String refundBillNo1 = applyRefundReq.getSalesRefundInfoList()
					.get(0).getRefundBillNo();

			salesrefundfailreq.setOrderId(order.getOrderId());
			salesrefundfailreq.setTopspeedRefund(true);
			salesrefundfailreq.setRefundBillNo(refundBillNo1);
			salesrefundfailCall.setData(salesrefundfailreq);
			salesrefundfailCall.callService();

			Logger.verifyEquals("true",
					salesrefundfailCall.getString("success"), "验证拒绝退货成功接口返回正常");

			// 数据库验证
			if (salesrefundfailCall.getString("success").equals("true")) {

				int i = 0;
				// 数据库验证
				for (SalesRefundInfoVo refund : applyRefundReq
						.getSalesRefundInfoList()) {

					String refundBillNo = refund.getRefundBillNo();

					RefundBillPo expectedRefundBillPo = new RefundBillPo();

					expectedRefundBillPo.setRefundBillNo(refundBillNo);
					expectedRefundBillPo.setCatalogId(refund.getCatalogId());
					expectedRefundBillPo.setTotalAmount(refund
							.getRefundAmount());
					expectedRefundBillPo.setProductCount(refund
							.getRefundProductNum());

					expectedRefundBillPo.setFreight(order
							.getOrderDetailPoList().get(i).getFreight());

					expectedRefundBillPo
							.setOrderId(applyRefundReq.getOrderId());
					expectedRefundBillPo.setUserId(placeOrderResp.getUserId());
					expectedRefundBillPo.setSellerId(order.getSellerId());
					expectedRefundBillPo.setTopspeedRefund(false);
					expectedRefundBillPo.setAfterReceiving(false);
					expectedRefundBillPo.setRefundStatus(0);
					expectedRefundBillPo.setTopspeedRefund(true);

					expectedRefundBillPo.setRefundBillStatus(-10);

					RefundBillPo actualRefundBillPo = orderShardingWapper
							.selectRefundBillByBillNo(refundBillNo);

					RefundService.verifyRefundBillDb(expectedRefundBillPo,
							actualRefundBillPo);

					i++;
				}
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
